import React from "react";
import BaseComponent from "../../redare/components/BaseComponent";
import {Button, Card, Col,Row,PageHeader} from "antd";
import GeneratorSysPresenter from "../../.gen/presenter/GeneratorSysPresenter";
import {WebPage} from "../../redare_components/webplatform_workbench/utils/WorkbenchDecoratorUtil";
import GeneratorSysForm from "../../components/generatorsys/GeneratorSysForm";

const routes = [
    {
        path: '/',
        breadcrumbName: '首页',
    },
    {
        path: '/generatorsys/add',
        breadcrumbName: '业务系统添加',
    }
];

export default @WebPage({
    id: 'com.redare.devframework.webplatform.pages.generatorsys.GeneratorSysFormPage',
    name: '业务系统添加',
    webPath: '/generatorsys/add',
    webFunIds: [
    ]
})

class GeneratorSysFormPage extends BaseComponent{

    constructor(props) {
        super(props);
        this.presenter = new GeneratorSysPresenter(this);
    }

    componentDidMount() {
        let {id} = this._urlQuery
        if (id) {
            this.showSpin()
            this.presenter.getGeneratorSys({id}, {}, (result) => {
                if (result&&this.form) {
                    let data = result.data;
                                                                                                                                this.form.setFormData(data)
                }
            })
        }
    }

    _submitForm() {
        if (this.form) {
            this.form.validateFormFields().then((values) => {
                this.showDoingIndicator();
                if (this._urlQuery.id) {
                    values.id = this._urlQuery.id;
                    this.presenter.editGeneratorSys(values);
                    return;
                }
                this.presenter.addGeneratorSys(values);
            });
        }
    }

    _onEditGeneratorSysSuccess(){
        this._onAddGeneratorSysSuccess();
    }

    _onAddGeneratorSysSuccess() {
        this.routeBack(this.props.history)
        this.sendEvent('generatorSysAddOk')
    }

    render() {
        return (
        <div className={'no-calc-footer-height'}>
            <PageHeader onBack={() => this._onBack()}  className="bg-white" title={this._urlQuery.id?'业务系统编辑':'业务系统新增'} routes={routes}/>
            {this.renderSpin(
                <div>
                    <Card bordered={false} className="margin24">
                        <GeneratorSysForm ref={(o) => {
                            this.form = o
                        }}/>
                    </Card>
                    <div className="footer bg-white">
                        <Row gutter={24} justify="end">
                            <Col>
                            <Button type="primary"
                                    onClick={this._submitForm.bind(this)}
                                    loading={this.state.submitBtnLoading}>保存</Button>
                            </Col>
                        </Row>
                    </div>
                </div>
            )}
        </div>
        )
    }

    _onBack() {
        this.routeBack(this.props.history)
    }

    showLoadingIndicator(text, options = {}) {
        this.showSpin(text,options);
    }

    closeLoadingIndicator() {
        this.closeSpin();
    }

    closeDoingIndicator(){
        this.setState({submitBtnLoading:false})
    }

    showDoingIndicator(text, options = {}) {
        this.setState({submitBtnLoading:true})
    }
}





